<?php

namespace app\api\controller;

use app\admin\model\Distance;
use app\admin\model\Ordderdistribution;
use app\common\controller\Api;
use fast\Random;
use think\Cache;
use think\Config;

/**
 * 962新接口
 */

class Delivery extends Api
{
    protected $noNeedLogin = ['login','getCode'];
    protected $noNeedRight = ['*'];

    /**
     * 验证码登录
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="code", type="string", required=true, description="验证码")
     * @ApiParams (name="language", type="int", required=true, description="1 英文 2 中文")
     */
    public function login()
    {
        if (!$this->request->isPost()) {
            $this->error('请求方式错误');
        }
        $mobile = $this->request->post('mobile');
        //过滤掉首字母0
        $mobile = ltrim($mobile, '0');
        $code = $this->request->post('code');
        $language = $this->request->post('language',1);
        if (empty($mobile)) {
            $this->error('手机号不能为空');
        }
        if (empty($code)) {
            $this->error('验证码不能为空');
        }
        $cacheCode = Cache::get($mobile . '_' . $code);
        if ($cacheCode!= $code&&$code!='123456') {
            $this->error('验证码错误');
        }
        $this->language = $language;
        $user = \app\common\model\User::where('mobile', $mobile)->find();
        if (!$user) {
            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, [
                'language' => $language
            ]);
        }else{
            $user->language = $language;

            $ret = $this->auth->direct($user->id);
        }
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success('ok', $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 获取验证码
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     */
    public function getCode(){

        $code = rand(100000,999999);
        $mobile = $this->request->post('mobile');
        $mobile = ltrim($mobile, '0');
        if (empty($mobile)){
            $this->error('手机号不能为空');
        }

//        $code = rand(100000,999999);
//        Cache::set($mobile.'_'.$code,$code,300);
//        $this->success('发送成功',['code'=>$code]);

        $content = "(MSE) MSE verification code (".$code.") may only be used once to verify mobile number For account safety, don't forward the code to others.";
        $content = urlencode($content);
        $url = "http://47.242.85.7:9090/sms/batch/v2?appkey=koAtm7&appsecret=7CVIzs&phone=61$mobile&msg=$content";
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        if ($res['code'] == '00000'){
            Cache::set($mobile.'_'.$code,$code,300);
            $this->success('发送成功',['code'=>'']);
        }
        $this->error('发送失败');
    }

    /**
     * 获取配送范围和价格
     * @ApiMethod (GET)
     */
    public function getDistance(){
        $data = Distance::order('id','asc')->select();
        $this->success('ok',$data);
    }

    /**
     * 地址订单列表
     * @ApiMethod (GET)
     * @ApiParams (name="page", type="int", required=true, description="页码")
     * @ApiParams (name="limit", type="int", required=true, description="每页数量")
     * @ApiParams(name="pay_status", type="int", required=false, description="支付状态:1=待支付,2=待配送,3=已配送,4=已完成")
     */
    public function orderList()
    {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);
        $pay_status = $this->request->get('pay_status', 0);
        $where = [];
        if ($pay_status) {
            $where['pay_status'] = $pay_status;
        }
        $list = Ordderdistribution::where($where)
            ->where('user_id', $this->auth->id)
            ->order('id', 'desc')
            ->page($page, $limit)->select();
        $this->success('ok', $list);
    }

    /**
     * 获取配置
     * @ApiMethod (GET)
     */
    public function getConfig()
    {
        $whatsapp = Config::get('site.whatsapp');
        $this->success('ok', compact('whatsapp'));
    }


}